import SectionLoadingMixin from './section-loading'
import { get } from 'lodash'
const LIMIT = 10
const DEFAULT_TOTAL = 0
export default {
  mixins: [SectionLoadingMixin],
  data() {
    return {
      page: 1,
      datas: [],
      limit: LIMIT,
      total: DEFAULT_TOTAL
    }
  },
  methods: {
    async fetchData(params = {}) {
      this.loading()
      try {
        const res = await this.getDatas(params)
        if (
          res &&
          res.code === 0 &&
          res.rows &&
          Array.isArray(res.rows)
        ) {
          this.datas = res.rows
          this.total = res.total
        }
      } catch (e) {
        this.datas = []
        this.resetPage()
      } finally {
        this.loaded()
      }
    },
    getDatas() {
      return Promise.resolve([])
    },
    resetData() {
      this.datas = []
      this.resetPage()
      this.fetchData()
    },
    resetPage() {
      this.limit = LIMIT
      this.total = DEFAULT_TOTAL
    }
  }
}
